package com.dkd.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VO.GoodsVo;
import com.dkd.manage.domain.VO.SaleStatisticsVo;
import com.dkd.manage.domain.VO.TaskStatisticsVo;
import com.dkd.manage.domain.VO.VmDetailVo;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.OrderMapper;
import com.dkd.manage.mapper.TaskMapper;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVendingMachineService;
import com.dkd.manage.service.IVmTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备管理Service业务层处理
 * 
 * @author itheima
 * @date 2025-01-22
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private IVmTypeService vmTypeService;

    @Autowired
    private INodeService nodeService;

    @Autowired
    private IChannelService channelService;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VmDetailVo selectVendingMachineById(Long id)
    {
        VendingMachine vendingMachine = vendingMachineMapper.selectVendingMachineById(id);
        VmDetailVo detailVo = new VmDetailVo();
        BeanUtil.copyProperties(vendingMachine, detailVo);
        // 获取设备编号
        if(vendingMachine != null){
            String innerCode = vendingMachine.getInnerCode();
            // 获取销售额和销售量
            SaleStatisticsVo saleStatisticsVo = orderMapper.selectGoodsVoByInnerCode(innerCode);
            if(saleStatisticsVo != null){
                detailVo.setSaleNum(saleStatisticsVo.getSaleNum());
                detailVo.setSaleMoney(saleStatisticsVo.getSaleMoney());
            }
            // 获取补货次数和维修次数
            TaskStatisticsVo taskStatisticsVo = taskMapper.selectTaskStatisticsVo(innerCode);
            if(taskStatisticsVo != null){
                detailVo.setSupplyNum(taskStatisticsVo.getSupplyNum());
                detailVo.setRepairNum(taskStatisticsVo.getRepairNum());
            }
            // 获取商品销售统计列表
            List<GoodsVo> goodsVoList = orderMapper.getMonthlySales(innerCode);
            detailVo.setGoodsSaleNumMonth(goodsVoList);
        }
        return detailVo;
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        // -- 新增设备 --
        // 1.生成8位的唯一标识，补充货道编号
        String innerCode = UUIDUtils.getUUID();
        // 2.查询售货机类型表，补充设备容量
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        // 3.查询点位表，补充区域点位合作商信息
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtil.copyProperties(node, vendingMachine,"id");
        vendingMachine.setAddr(node.getAddress());
        // 设备状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY); // 0-设备状态：0-未投放
        vendingMachine.setInnerCode(innerCode);
        // 创建时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        // 更新时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        // -- 新增货道 --
        List<Channel> channels = new ArrayList<>();
        // 双层for循环
        for (int i = 1; i < vmType.getVmRow(); i++) {
            for (int j = 0; j < vmType.getVmCol(); j++) {
                // 封装channel对象
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j);
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(innerCode);
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                // 插入货道
                channels.add(channel);
            }
        }
        // 批量新增货道
        channelService.insertChannels(channels);

        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        // 查询点位表，补充区域，点位，合作商等信息
        if(vendingMachine.getNodeId() != null){
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtil.copyProperties(node, vendingMachine,"id");
            vendingMachine.setAddr(node.getAddress());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }


    /**
     * 根据内部编号查询设备信息
     * @param innerCode 设备编号
     * @return 设备信息
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
